package com.LeeCode;

import java.util.Deque;
import java.util.LinkedList;

/**
 * 二叉树的堂兄弟节点
 */

public class Code993 {
    public static void main(String[] args) {

    }

    public boolean isCousins(TreeNode root, int x, int y) {
        Deque<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            int size = queue.size();
            boolean flagX = false, flagY = false;
            for (int i = 0; i < size; i++) {
                TreeNode node = queue.poll();
                if (node.left != null) queue.offer(node.left);
                if (node.right != null) queue.offer(node.right);
                if (node.left != null && node.right != null) {
                    if (node.left.val == x && node.right.val == y || node.left.val == y && node.right.val == x)
                        return false;
                }
                if (node.val == x) flagX = true;
                if (node.val == y) flagY = true;
                if (flagX && flagY) return true;
            }
        }
        return false;
    }
}
